package mapEditor.obj;

import debug.*;

import mapEditor.svg.SVGDOMTree;
import mapEditor.utils.MapLocation;
import mapEditor.utils.SvgFill;
import mapEditor.utils.SvgStroke;
import mapEditor.utils.Util;

import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Dimension;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.net.URL;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPanel;

import org.w3c.dom.svg.SVGImageElement;
import org.w3c.dom.svg.SVGAnimatedLength;
import org.w3c.dom.svg.SVGLength;


//DOC: http://www.w3.org/TR/SVG11/struct.html#ImageElement
//DOC: http://www.zvon.org/xxl/svgReference/Output/el_line.html
public class ImageObject extends DrawObject
{ private String uri;

  public ImageObject(SVGDOMTree domTree,MapLocation location1,MapLocation location2,String uri)
  { super(domTree,domTree.appendSVGImageElement());
    stroke.setEnabled(true);
	add(location1);
	add(location2);
	this.uri=uri;
	this.setClosedPath(true);
  }
  
  public ImageObject(SVGDOMTree domTree,SVGImageElement svgElement)
  { super(domTree,svgElement);
	decodeSVG(svgElement);
  }
  
  public URL getURL() throws IOException
  { URL url=new URL(uri);
    return(url);
  }
  
  public void decodeSVG(SVGImageElement svgElement)
  { Debug.TRACE("Image.decodeSVG: svgElement="+svgElement);
    this.setIdent(svgElement.getAttribute("id"));
    this.uri=svgElement.getAttributeNS("http://www.w3.org/1999/xlink","href");
    Debug.TRACE("Image.decodeSVG: uri="+uri);
    this.fill=new SvgFill(svgElement);
    this.stroke=new SvgStroke(svgElement);
    clearPointSet();
//    double x=Double.parseDouble(svgElement.getAttribute("x"));
//    double y=Double.parseDouble(svgElement.getAttribute("y"));
//    double width=Double.parseDouble(svgElement.getAttribute("width"));
//    double height=Double.parseDouble(svgElement.getAttribute("height"));
    SVGAnimatedLength alX=svgElement.getX();
    SVGAnimatedLength alY=svgElement.getY();
    SVGAnimatedLength alWidth=svgElement.getWidth();
    SVGAnimatedLength alHeight=svgElement.getHeight();
    SVGLength aX=alX.getAnimVal();
    SVGLength aY=alY.getAnimVal();
    SVGLength aWidth=alWidth.getAnimVal();
    SVGLength aHeight=alHeight.getAnimVal();
    double x=aX.getValue();
    double y=aY.getValue();
    double width=aWidth.getValue();
    double height=aHeight.getValue();
    
    double x0=x;
    double y0=y;
    double x1=x+width;
    double y1=y+height;

    add(MapLocation.newPixelPoint(domTree.getMapGrid(),x0,y0));
    add(MapLocation.newPixelPoint(domTree.getMapGrid(),x1,y1));
    setClosedPath(true);
  }

  protected Shape getShape(double dx,double dy)
  { MapLocation p0=getMapLocation(0);
    MapLocation p1=getMapLocation(1);
    double x0=p0.getX()+dx;
    double y0=p0.getY()+dy;
    double x1=p1.getX()+dx;
    double y1=p1.getY()+dy;
    double x=Math.min(x0,x1);
    double y=Math.min(y0,y1);
    double width=Math.abs(x1-x0);
    double height=Math.abs(y1-y0);
    Rectangle2D shape=new Rectangle2D.Double(x,y,width,height);
    return(shape);	  
  }
  
  public void directUpdateSVGElement()
  {	MapLocation p0=getMapLocation(0);
    MapLocation p1=getMapLocation(1);
    double x0=p0.getX();
    double y0=p0.getY();
    double x1=p1.getX();
    double y1=p1.getY();
    double x=Math.min(x0,x1);
    double y=Math.min(y0,y1);
    double width=Math.abs(x1-x0);
    double height=Math.abs(y1-y0);
    Debug.TRACE("Image.updateSVGElement: svgElement="+svgElement);
    svgElement.setAttributeNS(null,"id",getIdent());
    svgElement.setAttributeNS(null,"x",""+x);
    svgElement.setAttributeNS(null,"y",""+y);
    svgElement.setAttributeNS(null,"width",""+width);
    svgElement.setAttributeNS(null,"height",""+height);
    String xlinkNS="http://www.w3.org/1999/xlink";
//    svgElement.setAttributeNS(xlinkNS,"href",uri);
    svgElement.setAttributeNS(xlinkNS,"xlink:href",uri);
//    { // TODO: TESTING
//      SVGOMImageElement image=(SVGOMImageElement)svgElement;
//      SVGAnimatedString href=image.getHref();
//      String baseVal=href.getBaseVal();
//      Debug.TRACE("Image.updateSVGElement: href.baseVal="+baseVal);
////      href.setBaseVal(uri);
//      Debug.TRACE("Image.updateSVGElement: href.baseVal="+href.getBaseVal());
//    }
    fill.appendSVGAttributes(svgElement);
    stroke.appendSVGAttributes(svgElement);
  }
  
  
  // ****************************************************************
  // *** INNER CLASS: EditGeneralPanel
  // ****************************************************************
  public JPanel getEditGeneralPanel(String title)
  { return(new EditGeneralPanel(title)); }
  private class EditGeneralPanel extends JPanel
  { static final long serialVersionUID=0; // AD'HOC
    private JTextField identField;
    private JButton strokeButton;
    private JButton fillButton;
    
    public void paint(Graphics g)
    { Debug.TRACE("Path.EditGeneralPanel.paint: ");
      identField.setText(getIdent());
      strokeButton.setBackground(stroke.getColor());
      fillButton.setBackground(fill.getColor());
  	super.paint(g);  
    }
  	
    public EditGeneralPanel(String title)
    { setBorder(Util.getTitleBorder(title));
  	  setPreferredSize(new Dimension(350,120));
      setLayout(new GridLayout(5,2));
      add(new JLabel("       Ident:"));
      identField=new JTextField(getIdent());
      identField.addActionListener(new ActionListener()
      { public void actionPerformed(ActionEvent e)
        { setIdent(identField.getText()); }
      });
      add(identField);
      fillButton=addEditFillButton(this,true);
      strokeButton=addEditStrokeButton(this,true);
    }
 }

}
